package com.android.progressdemo;

import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

public class ProgressLayout extends LinearLayout {
    // 悬浮窗的高度，单位dp
    private static final float INFO_HEIGHT = 40;
    // 进度条的高度，单位dp
    private static final float PROGRESS_HEIGHT = 15;
    // 进度条距离悬浮窗的间隔距离，单位dp
    private static final float TOP_DISTANCE = 10;
    // 悬浮窗控件
    private InfoView infoView;
    // 进度条控件
    private ProgressView progressView;

    public ProgressLayout(Context context) {
        this(context, null);
    }

    public ProgressLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ProgressLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOrientation(LinearLayout.VERTICAL);
        setClipChildren(false);
        setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        initView(context);
    }

    private void initView(Context context) {
        // 悬浮窗
        infoView = new InfoView(context);
        infoView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, Utils.dp2px(context, INFO_HEIGHT)));
        addView(infoView);

        // 进度条
        progressView = new ProgressView(context);
        LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, Utils.dp2px(context, PROGRESS_HEIGHT));
        layoutParams.setMargins(0, Utils.dp2px(context, TOP_DISTANCE), 0, 0);
        progressView.setLayoutParams(layoutParams);
        addView(progressView);
    }


    /**
     * 设置进度，带动画
     *
     * @param progress 当前进度
     */
    public void setProgressWithAnim(float progress) {
        infoView.setProgressWithAnim(progress);
        progressView.setProgressWithAnim(progress);
    }
}
